package com.example.zce.back.controller.sys;

import com.example.zce.library.common.bean.ResultMapBean;
import com.example.zce.library.common.bean.ResultTableBean;
import com.example.zce.library.data.sys.entity.BackRole;
import com.example.zce.library.data.sys.entity.BackUser;
import com.example.zce.library.data.sys.model.BackUserModel;
import com.example.zce.library.data.sys.service.BackRoleService;
import com.example.zce.library.data.sys.service.BackUserService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 描述：管理员控制器
 *
 * @author zengyz
 * @date 2018/7/27 0:11
 */
@Controller
@RequestMapping("/sys/user")
public class BackUserController {

    @Autowired
    private BackUserService backUserService;

    @Autowired
    private BackRoleService backRoleService;

    /**
     * 首页
     * @return
     */
    @GetMapping("/index.html")
    public ModelAndView indexHtml(){
        return new ModelAndView("sys/user-index");
    }

    /**
     * 分页数据
     * @param request
     * @return
     */
    @ResponseBody
    @GetMapping("/list.json")
    public ResultTableBean<BackUserModel> listJson(HttpServletRequest request){
        Map params=new HashMap();
        params.put("pageNum",request.getParameter("page"));
        params.put("pageSize",request.getParameter("limit"));

        String account = request.getParameter("account");
        if (StringUtils.isNotEmpty(account)){
            params.put("account","%"+account+"%");
        }

        String sTime = request.getParameter("sTime");
        if (StringUtils.isNotEmpty(sTime)){
            params.put("sTime",sTime);
        }

        String eTime = request.getParameter("eTime");
        if (StringUtils.isNotEmpty(eTime)){
            params.put("eTime",eTime);
        }

        ResultTableBean<BackUserModel> resultTableBean = backUserService.findByParams(params);
        return resultTableBean;
    }

    /**
     * 添加页面
     * @return
     */
    @GetMapping("/add.html")
    public ModelAndView addHtml(){
        List<BackRole> roles = backRoleService.findByAll();
        return new ModelAndView("sys/user-add","roles",roles);
    }

    /**
     * 修改页面
     * @param request
     * @return
     */
    @GetMapping("/update.html")
    public ModelAndView updateHtml(HttpServletRequest request){
        String id = request.getParameter("id");
        BackUser backUser = backUserService.findById(id);
        List<BackRole> roles = backRoleService.findByAll();
        request.setAttribute("backUser",backUser);
        request.setAttribute("roles",roles);
        return new ModelAndView("sys/user-update");
    }

    /**
     * 保存和修改
     * @param backUser
     * @return
     */
    @ResponseBody
    @PostMapping("/saveupdate.json")
    public ResultMapBean saveupdateJson(BackUser backUser){
        try {
            int i = backUserService.saveOrUpdate(backUser);
            if (i!=0){
                return ResultMapBean.ok();
            }else{
                return ResultMapBean.error(501,"操作失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return ResultMapBean.error(500,"系统错误");
        }
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @ResponseBody
    @PostMapping("/delete.json")
    public ResultMapBean deleteJson(String id){
        try {
            int i = backUserService.delete(id);
            if (i!=0){
                return ResultMapBean.ok();
            }else{
                return ResultMapBean.error(501,"操作失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return ResultMapBean.error(500,"系统错误");
        }
    }

}
